package app.mvc.model.proxys
{
	import alternativa.gui.data.DataProvider;
	
	import app.consts.CommandType;
	import app.mvc.Notifications;
	import app.mvc.model.VO.ControlsBarVO;
	import app.mvc.model.VO.LabelVO;
	
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.Proxy;
	
	import globals.Config;
	
	import org.puremvc.as3.interfaces.IProxy;
	import org.puremvc.as3.patterns.facade.Facade;
	
	import utils.Tracker;
	
	public class AppMainProxy extends Proxy implements IProxy
	{
		public static const NAME:String = "AppMain";
		
		private var labelsVO:DataProvider;
		private var controlsVOs:DataProvider;
		public function AppMainProxy()
		{
			super();
			
			labelsVO = new DataProvider();
			controlsVOs = new DataProvider();
		}
		
		public function loadConfig():void
		{
			var urlLoader:URLLoader = new URLLoader(new URLRequest(Config.RES_PRE_URL+"xml/config.xml"));
			urlLoader.addEventListener(Event.COMPLETE,function(evt:Event):void{
				Config.configData = new XML(evt.target.data);
				Config.moveStep = Config.configData.editor.moveStep;
				
				labelsVO.removeAll();
				parseNode(Config.configData.interfaces.labelsBar.label);

				parseControlsVOs(Config.configData.interfaces.controlBar.group);
				
				Facade.getInstance().sendNotification(Notifications.PROXY_APP_LABELS_BAR_PARSED,labelsVO);		//初始化标签栏
				
				Facade.getInstance().sendNotification(Notifications.PROXY_APP_CONTROLS_BAR_PARSED,controlsVOs);		//初始化控件条
				
				Facade.getInstance().sendNotification(Notifications.CMD_TO_CHANGE_BACKGROUND,Config.RES_PRE_URL+"images/sango_bg.png",CommandType.NOT_IN_HISTORY);
			});
		}
		
		/**
		 * 将控件栏数据转换成可用VO
		 * @param $data
		 */
		private function parseControlsVOs($data:XMLList):void
		{
			controlsVOs.removeAll();
			var len:int = $data.length();		//大类group的数量
			var xmlItem:XML = null;
			var len2:int=0;
			var items:XMLList;
			for(var i:int=0;i<len;++i)
			{
				var vo:ControlsBarVO = new ControlsBarVO();
				vo.itemId=i+"";
				vo.parentId = null;
				vo.label = $data[i].@name;
					vo.opened = true;
				vo.level = 0;
				vo.hasChildren = true;
				vo.canExpand = true;
				controlsVOs.addItem(vo);
				items = $data[i].item;
				len2 = items.length();
				for (var j:int = 0; j < len2; ++j) {
					xmlItem = items[j];
					var vo2:ControlsBarVO = new ControlsBarVO();
					vo2.parentId = String(i);
					vo2.label = xmlItem.@label;
					vo2.opened = true;
					vo2.level = 1;
					vo2.hasChildren = false;
					vo2.canExpand = false;
					vo2.itemId=i+"_"+j;
					
//					vo2.name = xmlItem.@name;
//					vo2.width = int(xmlItem.@width);
//					vo2.height = int(xmlItem.@height);
//					vo2.label = xmlItem.@label;
//					vo2.desc = xmlItem.@desc;
//					vo2.classString = xmlItem.@classString;
//					vo2.isContainer = xmlItem.@isContainer;
					
					controlsVOs.addItem(vo2);
				}

			}
		}
		
		/**
		 * 递归遍历XML组成数据放入标签栏组件
		 * @param $data
		 * @param $depth
		 * @param $parent
		 */
		private function parseNode($data:XMLList,$depth:int=0,$parent:LabelVO=null):void
		{
			var len:int = $data.length();
			var obj:LabelVO = null;
			var parentObj:LabelVO=null;
			for(var i:int=0;i<len;++i)
			{
				obj = new LabelVO();
				if($depth==0)
				{
					labelsVO.addItem(obj);
				}
				parentObj=renderNode(obj,$data[i],i,$depth,$parent);
				if($data[i].children().length())
				{
					parseNode($data[i].label,$depth+1,parentObj);
				}
			}
		}
		/**
		 * 设置单个标签节点值
		 * @param $nodeObj
		 * @param $nodeXML
		 * @param $index
		 * @param $depth
		 * @param $parentNode
		 * @return 
		 */
		private function renderNode($nodeObj:LabelVO,$nodeXML:XML,$index:int,$depth:int,$parentNode:LabelVO):LabelVO
		{
			$nodeObj.label = $nodeXML.@name;
			$nodeObj.command = $nodeXML.@command;
			$nodeObj.items = new DataProvider();
			if($depth && $parentNode)
			{
				$nodeObj.id = $parentNode.id + '.' + $index;
				$nodeObj.parent = $parentNode;
				$parentNode.items.addItem($nodeObj);
			}
			else
			{
				$nodeObj.id = $index+"";
			}
			return $nodeObj;
		}		

		

		
		public function getProxyName():String
		{
			return NAME;
		}
		
		public function setData(data:Object):void
		{
		}
		
		public function getData():Object
		{
			return null;
		}
		
		public function onRegister():void
		{
		}
		
		public function onRemove():void
		{
		}
	}
}